import * as api from '@/api/sourcing/purchaseInquiry'

const state = {
  demandList: [],
  orderList: [],
  sourcingSupplierList: [],
  supplierList: [],
  supplierId: '',
  sourcingSupplierId: '',
  isCreateOrdernum: '',
  getskuinforList: {},
  total: 0,
  ordertotal: 0,
  demandTableLoading: false,
  orderTableLoading: false,
  pushDownLoading: false,
  settleCompanyList: [], // 结算公司列表
  contractList: [], // 订货询价合同列表
}
const mutations = {
  SET_SOURCINGSUPP_LIST: (state, result) => {
    state.sourcingSupplierList = result
    state.supplierList = result[0] && result[0].sourcingSkuDTOList
    state.supplierId = result[0].supplierId
    state.isCreateOrdernum = result[0].isCreateOrder
    state.sourcingSupplierId = result[0].sourcingSupplierId
  },
  SET_SUPPLIER_LIST: (state, result) => {
    state.supplierList = result.sourcingSkuDTOList
    state.supplierId = result.supplierId
    state.sourcingSupplierId = result.sourcingSupplierId
    state.isCreateOrdernum = result.isCreateOrder
  },
  SET_GET_SKUINFOR_LIST: (state, result) => {
    state.getskuinforList = result
    state.settleCompanyList = result.settleCompanyList || []
  },
  SET_SETTLE_COMPANY_LIST: (state, result) => {
    state.settleCompanyList = result || []
  },
}
const actions = {
  // 下推采购订单 - 获取结算公司列表
  getSettlementCompanyList ({ state, commit }, { payload: { data, cb } }) {
    api.getSettlementCompanyListService(data).then(res => {
      if (cb) {
        cb(res)
      }
      commit('SET_SETTLE_COMPANY_LIST', res.data ? res.data : [])
    }).catch(() => {
      commit('SET_SETTLE_COMPANY_LIST', [])
    })
  },
  // 下推页 -- 列表
  pushDownPageList ({ state, commit }, { payload: { data } }) {
    return new Promise((resolve, reject) => {
      api.pushdownService(data).then(res => {
        commit('SET_PUSHDOWN_LIST', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 下推页面 ---地址
  getAddressListList ({ commit, state }, { payload: { data, cb } }) {
    api.getAddressListService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 下推页面----货物处理方式
  getSysTypeInfoList ({ commit, state }, { payload: { data, cb } }) {
    api.getSysTypeInfoService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 下推页面----收货公司、转回公司
  getSonCompanyList ({ commit, state }, { payload: { data, cb } }) {
    api.getSonCompanyListService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 下推页面----查询供应商接口
  queryBillSupplierList ({ commit, state }, { payload: { data, cb } }) {
    api.queryBillSupplierService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 中间页----定标信息列表查询-供应商维度
  querySourcingSupplierList ({ commit, state }, { payload: { data, cb } }) {
    api.querySourcingSupplierService(data).then((res) => {
      if (cb) {
        cb(res);
      }
      commit('SET_SOURCINGSUPP_LIST', res.data ? res.data : [])
    });
  },
  // 下推页面----详情信息
  getWinSourcingSKUInforList ({ commit, state }, { payload: { data, cb } }) {
    api.getWinSourcingSKUInforService(data).then((res) => {
      if (cb) {
        cb(res);
      }
      commit('SET_GET_SKUINFOR_LIST', res.data)
    });
  },
  // 下推页面-----生成下推采购订单
  sendPurchaseOrderList ({ commit, state }, { payload: { data, cb } }) {
    state.pushDownLoading = true;
    api.sendPurchaseOrderService(data).then((res) => {
      state.pushDownLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.pushDownLoading = false;
    });
  },
  // 下推采购订单时校验是否需审批
  checkPurchaseOrderPushOa ({ commit, state }, { payload: { data, cb } }) {
    state.pushDownLoading = true;
    api.checkPurchaseOrderPushOaService(data).then((res) => {
      state.pushDownLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.pushDownLoading = false;
    });
  },
  // 下推页面----采购订单类型
  getSysDictByTypeList ({ commit, state }, { payload: { data, cb } }) {
    api.getSysDictByTypeService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
  // 订货---判断单据下商品是否为待二次询价
  checkSecondPriceList ({ commit, state }, { payload: { data, cb } }) {
    api.checkSecondPriceService(data).then((res) => {
      if (cb) {
        cb(res);
      }
    });
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
